interface Options {
    method: string
    url: string
    data: object
    dataType: string
}
/* 自制jsonp -- 支持跨域 */
class Jsonp {
    /* 解决跨域请求 */
    createScriptTag(url: string, data: object) {
        const oScript = document.createElement('script');
        oScript.id = "script1";
        oScript.src = url + '?' + data + '&callback=fn';
        document.body.appendChild(oScript);
    }
    constructor(opt: Options) {
        opt = opt || {};
        opt.method = opt.method || 'POST';
        opt.url = opt.url || '';
        opt.data = opt.data || null;
        opt.dataType = opt.dataType || 'JSONP';
        const params: any = [];
        Object.keys(opt.data).forEach((key: any) => {
            const cur = Object.values(opt.data)[key]
            params.push(key + '=' + cur);
        })
        const postData = params.join('&');
        if (opt.dataType === 'JSONP') {
            this.createScriptTag(opt.url, postData);
        }
    }
}

const jsonp = (opt: Options) => {
    return new Jsonp(opt)
}
export default jsonp
